package com.feng.common.utils;

import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@UtilityClass
public class TimeUtils {

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_DAY_FORMAT = "yyyy-MM-dd";
    public static final String DATE_TIME_FORMAT_SQL = "yyyyMMddHHmmss";
    public static final String DATE_HOUR_FORMAT_SQL = "yyyyMMddHH";
    public static final String DATE_DAY_FORMAT_SQL = "yyyyMMdd";
    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final String LONG_TIME_FORMAT = "yyyyMMddHHmmssSSS";
    public static final String DATE_MONTH_FORMAT_SQL = "yyyyMM";
    public static final String DATE_HOUR_MIN_FORMAT_SQL = "yyyyMMddHHmm";
    public static final String DATE_DAY_FORMAT_SQL_TASK = "yyyy/MM/dd";
    public static final String DATE_MONTH_FORMAT = "yyyy-MM";
    public static final String DATE_DAY_FORMAT_H = "yyyy/MM/dd HH:mm:ss";

    /**
     * 根据格式转换时间字符串为时间4
     */
    public static Date getDateByFormat(String timeString, String format) throws ParseException {
        if (StringUtils.isEmpty(timeString) || StringUtils.isEmpty(format)) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.parse(timeString);
    }

    /**
     * 根据格式转换时间
     */
    public static Date getDateByFormat(Date dateTime, String format) {
        if (dateTime == null || format == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(dateTime);
        Date result = null;
        try {
            result = formatter.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取当前时间是几号
     */
    public static int getCurrentDayOfMonth() {
        //获取当前日期
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当前是周几
     */
    public static int getCurrentDayOfWeek() {
        //获取当前日期
        int dayOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
        if (dayOfWeek == 0) {
            dayOfWeek = 7;
        }
        return dayOfWeek;
    }

    /**
     * 根据格式获取当前时间字符串
     */
    public static String getCurrentDateString(String dateFormmat) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat(dateFormmat);
        return format.format(date);
    }

    /**
     * 获取特定格式的时间字符串
     */
    public static String getDateString(Date date, String formmat) {
        SimpleDateFormat format = new SimpleDateFormat(formmat);
        return format.format(date);
    }

    public static String getCalendarFormatTime(Calendar calendar, String formmatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formmatStr);
        return format.format(calendar.getTime());
    }

    public static String getDateStringFormmat(String dateString, String srcFormmat, String tarFormmat) throws ParseException {
        Date date = getDateByFormat(dateString, srcFormmat);
        return getDateString(date, tarFormmat);
    }

    /**
     * 根据格式计算时间
     */
    public static String calculateTime(String time, String formmat, int calculateNum, int type) throws ParseException {
        Date date = getDateByFormat(time, formmat);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, calculateNum);
        // 时间格式
        SimpleDateFormat format = new SimpleDateFormat(formmat);
        return format.format(calendar.getTime());
    }

    /**
     * 获取指定年月的第一天
     *
     * @param year  年
     * @param month 月
     */
    public static String getFirstDayOfMonth(int year, int month, String format) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(cal.getTime());
    }

    /**
     * 获取指定年月的最后一天
     *
     * @param year  年
     * @param month 月
     */
    public static String getLastDayOfMonth(int year, int month, String format) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR, year);
        //设置月份
        cal.set(Calendar.MONTH, month - 1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(cal.getTime());
    }

    /**
     * 获取当前月所属季度
     *
     * @param month 月份
     * @return 季度
     */
    public static int getQuarterNum(int month) {
        if (month >= 1 && month < 4) {
            return 1;
        } else if (month >= 4 && month < 7) {
            return 2;
        } else if (month >= 7 && month < 10) {
            return 3;
        } else {
            return 4;
        }
    }

    /**
     * 获取某月份所属季度的第一个月
     *
     * @param month 月份
     * @return 月份
     */
    public static int getQuarterStartMonth(int month) {
        if (month >= 1 && month < 4) {
            return 1;
        } else if (month >= 4 && month < 7) {
            return 4;
        } else if (month >= 7 && month < 10) {
            return 7;
        } else {
            return 10;
        }
    }

    /**
     * 获取某月份所属季度的最后一个月
     *
     * @param month 月份
     * @return 月份
     */
    public static int getQuarterEndMonth(int month) {
        if (month >= 1 && month < 4) {
            return 3;
        } else if (month >= 4 && month < 7) {
            return 6;
        } else if (month >= 7 && month < 10) {
            return 9;
        } else {
            return 12;
        }
    }

    /**
     * 比较两个时间的大小
     *
     * @param sourceTime 原时间
     * @param targetTime 目标时间
     * @return -1 小于  0等于 1大于
     */
    public static int compareTime(Date sourceTime, Date targetTime) {
        Calendar sourceCalendar = Calendar.getInstance();
        sourceCalendar.setTime(sourceTime);

        Calendar targetCalendar = Calendar.getInstance();
        targetCalendar.setTime(targetTime);

        return sourceCalendar.compareTo(targetCalendar);
    }

    /**
     * 计算两个日期相差多少个月
     *
     * @param startCalendar 开始时间
     * @param endCalendar   截止时间
     * @return 相差月份
     */
    public static int towDateDiffMonth(Calendar startCalendar, Calendar endCalendar) {
        //判断日期大小
        if (startCalendar.after(endCalendar)) {
            Calendar temp = startCalendar;
            startCalendar = endCalendar;
            endCalendar = temp;
        }
        int startYear = startCalendar.get(Calendar.YEAR);
        int endYear = endCalendar.get(Calendar.YEAR);
        int startMonth = startCalendar.get(Calendar.MONTH);
        int endMonth = endCalendar.get(Calendar.MONTH);
        return (endYear - startYear) * 12 + (endMonth - startMonth);
    }
}

